# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2024/1/7
desc: 
sample: 
"""
import json

from django_celery_beat.models import CrontabSchedule, PeriodicTask
from dvadmin_celery.views.task import CronSlpit, CeleryCrontabScheduleSerializer, PeriodicTasksSerializer
from rest_framework.exceptions import ValidationError

from apitest.models import EnvInfo, ProjectInfo, ModuleInfo, TestApiInfo, TestApiResult, TestCase
from dvadmin.utils import string_util
from dvadmin.utils.serializers import CustomModelSerializer



class EnvInfoModelSerializer(CustomModelSerializer):
    """
    序列化器
    """
    def validate_paramsbody(self,value):
        if string_util.is_json(value)==False:
            raise ValidationError('不是json格式')
        return value


    class Meta:
        model = EnvInfo
        fields = "__all__"


class ProjectInfoModelSerializer(CustomModelSerializer):

    def validate(self, value):
        print("test")
        # if string_util.is_json(value) == False:
        #     raise ValidationError('不是json格式')
        return value

    class Meta:
        model = ProjectInfo
        fields = "__all__"


class ModuleInfoSerializer(CustomModelSerializer):

    class Meta:
        model = ModuleInfo
        fields = "__all__"

class TestApiInfoSerializer(CustomModelSerializer):

    class Meta:
        model = TestApiInfo
        fields = "__all__"

    def validate(self, attr):
        headers = attr.get('headers', '')
        if headers != '':
            if string_util.is_json(headers) == False:
                raise ValidationError({'头部信息': '不是json格式'})
        jsonbody = attr.get('jsonbody', '')
        if jsonbody != '':
            if string_util.is_json(jsonbody) == False:
                raise ValidationError({'json参数': '不是json格式'})
        return attr


class TestApiResultSerializer(CustomModelSerializer):

    class Meta:
        model = TestApiResult
        fields = "__all__"


class TestCaseSerializer(CustomModelSerializer):

    def create(self, validated_data):
        case_data = super().create(validated_data)
        # 追加 定时任务逻辑
        cron = self.get_initial().get('cron')
        if cron is not None:
            # body_data = request.data.copy()
            # cron = body_data.get('crontab_str')
            cron_data = CronSlpit(cron)
            # 添加crontab
            serializer = CeleryCrontabScheduleSerializer(data=cron_data)
            serializer.is_valid(raise_exception=True)
            print(cron_data)
            schedule, created = CrontabSchedule.objects.get_or_create(**cron_data)  # 手动创建crontab，避免重复创建
            # 添加任务
            body_data = {}
            body_data['name'] = case_data.case_name
            body_data['crontab'] = schedule.id
            body_data['enabled'] = True
            env = self.request.data.get('env')
            # env = self.get_initial().get('env')
            body_data['kwargs'] = json.dumps({'case_id': case_data.id, 'evn_id': env.get('id')})
            body_data['task'] = 'apitest.tasks.apiTask'
            task_serializer = PeriodicTasksSerializer(data=body_data,
                                                      context={"crontab": serializer.data.get('id')})

            task_serializer.is_valid(raise_exception=True)
            task_serializer.save()
            case_data.task_id = task_serializer.data.get("id")
            case_data.save()
        return case_data

    def update(self, instance, validated_data):
        db_corn = instance.cron
        toupdate_corn = self.get_initial().get("cron")
        case_data = super().update(instance, validated_data)
        if toupdate_corn == '':
            toupdate_corn = None
        case_name = self.get_initial().get("case_name")

        # 新增corn
        if toupdate_corn is not None and instance.task_id is None:
            cron_data = CronSlpit(toupdate_corn)
            # 添加crontab
            serializer = CeleryCrontabScheduleSerializer(data=cron_data)
            serializer.is_valid(raise_exception=True)
            print(cron_data)
            schedule, created = CrontabSchedule.objects.get_or_create(**cron_data)  # 手动创建crontab，避免重复创建
            # 添加任务
            body_data = {}
            body_data['name'] = case_name
            body_data['crontab'] = schedule.id
            body_data['enabled'] = True
            env = self.request.data.get('env')
            # env = self.get_initial().get('env')
            body_data['kwargs'] = json.dumps({'case_id': instance.id, 'evn_id': env.get('id')})
            body_data['task'] = 'apitest.tasks.apiTask'
            task_serializer = PeriodicTasksSerializer(data=body_data,
                                                      context={"crontab": serializer.data.get('id')})

            task_serializer.is_valid(raise_exception=True)
            task_serializer.save()
            case_data.task_id = task_serializer.data.get("id")
            case_data.save()
            return case_data
        # 修改corn
        if case_data.task_id is not None and toupdate_corn is not None and toupdate_corn != db_corn:
            try:
                task = PeriodicTask.objects.get(id=case_data.task_id)
                cron_data = CronSlpit(toupdate_corn)
                schedule, created = CrontabSchedule.objects.get_or_create(**cron_data)  # 手动创建crontab，避免重复创建
                task.name=case_data.case_name
                task.crontab = schedule
                task.save()
            except Exception as e:
                # 删除
                case_data.task_id = None
                case_data.cron = None
                case_data.save()
            return case_data
        # 删除
        if case_data.task_id is not None and toupdate_corn is None:
            try:
                task = PeriodicTask.objects.get(id=case_data.task_id)
                task.delete()
            except Exception as e:
                print(e)
            case_data.task_id = None
            case_data.cron = None
            case_data.save()

        return case_data

    class Meta:
        model = TestCase
        fields = "__all__"
